package au.edu.swin.swinvehicle.manager;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * ManagerManagerBean
 * ----------------
 * This is the remote interface implementation of Manager
 *
 *
 * @author Dany Gunawan
 * @lastmodify 2009-05-12
 */
@Stateless
public class ManagerManagerBean implements ManagerManagerRemote {

    @PersistenceContext
    private EntityManager em;

    public void addManager(ManagerDetailedTO to) throws ManagerManagerException {
        try {
            ManagerEntity m = new ManagerEntity();
            m.setData(to);
            em.persist(m);
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot add Manager", e);
        }
    }

    public void editManager(ManagerDetailedTO to) throws ManagerManagerException {
        try {
            ManagerEntity m =  em.find(ManagerEntity.class, to.getId());
            m.setData(to);
            em.persist(m);
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot edit Manager", e);
        }
    }

    public void deleteManager(Long id) throws ManagerManagerException {

        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<ManagerTO> getAllManagers() throws ManagerManagerException {

        List<ManagerTO> results = null;
        try {
            results = new ArrayList<ManagerTO>();
            Query allManagersQuery = em.createNamedQuery("findAllManagers");
            List dbResults = allManagersQuery.getResultList();

            for (int i = 0; i < dbResults.size(); i++) {
                ManagerEntity m = (ManagerEntity) dbResults.get(i);
                results.add(m.getData());
            }
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot get all Managers", e);
        }

        return results;

    }

    public List<ManagerDetailedTO> getAllDetailedManagers() throws ManagerManagerException {
        List<ManagerDetailedTO> results = null;
        try {
            results = new ArrayList<ManagerDetailedTO>();
            Query allManagersQuery = em.createNamedQuery("findAllManagers");
            List dbResults = allManagersQuery.getResultList();

            for (int i = 0; i < dbResults.size(); i++) {
                ManagerEntity m = (ManagerEntity) dbResults.get(i);
                results.add(m.getDetailedData());
            }
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot get all ManagerDetailedTOs", e);
        }

        return results;

    }

    public ManagerDetailedTO getManager(Long id) throws ManagerManagerException {

        ManagerEntity m = null;
        ManagerDetailedTO to = null;

        try {
            m = em.find(ManagerEntity.class, id);
            to = m.getDetailedData();
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot get Manager by id " + id.longValue(), e);
        }

        return to;

    }
}
